using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;

[DisallowMultipleComponent]
public class ShopPanel : MonoBehaviour, LoopScrollPrefabSource, LoopScrollDataSource
{
    [SerializeField] private GameObject item;
    [Header("按钮组")]
    [SerializeField] private Transform sortButtonGroup;
    [SerializeField] private SecondaryButton secondaryButton;
    [SerializeField] private LoopScrollRect ls;
    Stack<Transform> pool = new Stack<Transform>();

    /// <summary>
    /// 格子遍历的数据
    /// </summary>
    private List<Item> currentItems = new List<Item>();

    private bool isAccendOrder;
    private void Start()
    {

        SecondaryButtonSet("种类", new List<ActionItem>
        {
            new ActionItem("全部",() => ShowInventory(ItemManager.instance.GetAllItems())),
            new ActionItem("道具",() => ShowInventory(ItemManager.instance.GetItemKinds("道具"))),
            new ActionItem("武器",() => ShowInventory(ItemManager.instance.GetItemKinds("武器")))
        });
        SecondaryButtonSet("排序", new List<ActionItem>
        {
            new ActionItem
            (
                "数量",() =>{
                    var sortedByCountAsc =Utility.Sort(currentItems, item => item.ItemInfo.count, isAccendOrder).ToList();
                    ShowInventory(sortedByCountAsc);
                    isAccendOrder=!isAccendOrder;
                    Debug.Log("按下数量件");
                    }
            )
        });

        Init();
    }

    private void Init()
    {
        //事件绑定
        ls.prefabSource = this;
        ls.dataSource = this;
    }
    private void ShowInventory(List<Item> items)
    {
        currentItems= items;

        RefreshData();
    }
    private void Update()
    {
        if(Input.GetKeyUp(KeyCode.Escape))
        {
            Debug.Log("测试跟新itemManger数据");
            ShowInventory(ItemManager.instance.GetAllItems());
        }
    }
    private void SecondaryButtonSet(string buttonName, List<ActionItem> Actions)
    {
        var clone = Instantiate(secondaryButton.gameObject, sortButtonGroup, false);
        clone.GetComponent<SecondaryButton>().Initialize(Actions);
        clone.GetComponent<SecondaryButton>().PrimaryButton(buttonName);
    }

    #region 接口
    public GameObject GetObject(int index)
    {
        if (pool.Count == 0)
        {
            return Instantiate(item);
        }
        Transform candidate = pool.Pop();
        candidate.gameObject.SetActive(true);
        return candidate.gameObject;
    }
    public void ReturnObject(Transform trans)
    {
        trans.SendMessage("ScrollCellReturn", SendMessageOptions.DontRequireReceiver);
        trans.gameObject.SetActive(false);
        trans.SetParent(ls.transform, false);
        pool.Push(trans);
    }
    /// <summary>
    /// 遍历每个格子
    /// </summary>
    /// <param name="transform"></param>
    /// <param name="idx"></param>
    public void ProvideData(Transform transform, int idx)
    {
        if (idx<currentItems.Count)
        {
            transform.GetComponent<ItemButton>().Init(currentItems[idx]);
        }
    }
    /// <summary>
    /// 修改物品时调用 刷新数据
    /// </summary>
    private void RefreshData()
    {
        //网格的总数
        ls.totalCount = currentItems.Count;
        //重新填充网格 当格子不够用时
        ls.RefillCells();
        //刷新数据 会重新调用 ProvideData 脚本
        ls.RefreshCells();
    }
    #endregion
}
